<?php
/**
 * GridView操作区
 */

namespace backend\utils;

use yii;
use yii\grid\ActionColumn;
use yii\helpers\Html;

class XColumn extends ActionColumn
{
    public $template = '{update} &nbsp;&nbsp; {delete}';
    public $header = '操作';

    protected function initDefaultButtons()
    {
        if (!isset($this->buttons['view'])) {
            $this->buttons['view'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'View'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                if (isset($_GET['page'])) {
                    $url .= '&page=' . Yii::$app->request->get('page');
                }
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
            };
        }

        if (!isset($this->buttons['update'])) {
            $this->buttons['update'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Update'),
                    'aria-label' => Yii::t('yii', 'Update'),
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                if (isset($_GET['page'])) {
                    $url .= '&page=' . Yii::$app->request->get('page');
                }
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['delete'])) {
            $this->buttons['delete'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', 'Delete'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', 'Are you sure you want to delete this item?'),
                    'data-method' => 'post',
                    'data-pjax' => '0',
                ], $this->buttonOptions);
                if (isset($_GET['page'])) {
                    $url .= '&page=' . Yii::$app->request->get('page');
                }
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
            };
        }
    }
}